package com.bzkj.web.controller.ordermanagement.outbound;

import com.alibaba.fastjson2.JSONObject;
import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.domain.NccRequestDTO;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.document.domain.DocConfirmDTO;
import com.bzkj.document.domain.otheroutorder.dto.IcGeneraloutH;
import com.bzkj.document.domain.otheroutorder.dto.IcGeneraloutRequestBody;
import com.bzkj.document.domain.otheroutorder.dto.OtherOutQueryDTO;
import com.bzkj.document.service.otheroutbound.OtherOutboundOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * @projectName: bzkj-wms
 * @package: com.bzkj.web.controller.ordermanagement
 * @className: OutboundOrderController
 * @description: 其他出库单据控制层
 * @author: C1309
 * @createDate: 2024年11月23日
 * @version: v1.0
 */
@RestController
@RequestMapping("/other-outbound")
public class OtherOutboundOrderController extends BaseController {

    /**
     * 获取NCC其他出库单据服务层
     */
    @Resource
    private OtherOutboundOrderService outboundOrderService;

    /**
     * 查询NCC其他出库单据
     *
     * @return 查询结果
     */
    @GetMapping("/ncc")
    @PreAuthorize("@ss.hasPermi('order:otherout:list')")
    public TableDataInfo selectNccOutboundOrder(@RequestParam Map<String, Object> requestBody) {
        NccRequestDTO nccRequestDTO = getNccRequestDTO(requestBody);
        if (nccRequestDTO.getFbillflag() == null || nccRequestDTO.getFbillflag().isEmpty()) {
            nccRequestDTO.setFbillflag("2");
        }
        return outboundOrderService.selectOutboundOrder(nccRequestDTO);
    }

    /**
     * 确认NCC其他出库单
     *
     * @param docConfirmDTO 确认信息
     * @return 确认结果
     */
    @PostMapping("/confirm")
    @PreAuthorize("@ss.hasPermi('order:otherout:confirm')")
    @LogB(title = "其他出库单", businessType = BusinessType.CONFIRM)
    public AjaxResult confirmOutboundOrder(@RequestBody DocConfirmDTO docConfirmDTO) {
        docConfirmDTO.setLoginUser(getLoginUser());
        return outboundOrderService.confirmOutboundOrder(docConfirmDTO);
    }

    /**
     * 获取本地其他出库单
     *
     * @param otherOutQueryDTO 其他出库单查询条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('order:otherout:list')")
    public TableDataInfo getLocalOtherOutboundOrder(OtherOutQueryDTO otherOutQueryDTO) {
        if (otherOutQueryDTO == null) {
            otherOutQueryDTO = new OtherOutQueryDTO();
        }
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return outboundOrderService.getLocalOtherOutboundOrder(otherOutQueryDTO, pageNum, pageSize);
    }

    /**
     * 新增其他出库单据
     *
     * @param responseBody 请求体
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('order:otherout:add')")
    @LogB(title = "其他出库单", businessType = BusinessType.INSERT)
    public AjaxResult addOtherOutboundOrder(@RequestBody String responseBody) {
        IcGeneraloutRequestBody icGeneraloutRequestBody = JSONObject.parseObject(responseBody, IcGeneraloutRequestBody.class);
        icGeneraloutRequestBody.setLoginUser(getLoginUser());
        IcGeneraloutH icGeneraloutH = icGeneraloutRequestBody.getIcGeneraloutH();
        icGeneraloutH.setCbizid(icGeneraloutH.getCwhsmanagerid());
        return outboundOrderService.addOtherOutboundOrder(icGeneraloutRequestBody);
    }

    /**
     * 修改其他出库单据
     *
     * @param responseBody 请求体
     * @return 修改结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('order:otherout:edit')")
    @LogB(title = "其他出库单", businessType = BusinessType.UPDATE, pkFieldNames = "cgeneralhid",
            tableNames = "wms_warehouse_out_other",
            detailTable = "wms_warehouse_out_detail_other"
    )
    public AjaxResult updateOtherOutBoundOrder(@RequestBody HashMap<String, Object> responseBody) {
        return outboundOrderService.updateOtherOutBoundOrder(responseBody, getLoginUser(), false);
    }

    /**
     * 其他出库单签字
     *
     * @param cgeneralhid 单据表头主键
     * @return 查询结果
     */
    @PutMapping("/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:otherout:audit')")
    @LogB(title = "其他出库单", businessType = BusinessType.SIGN)
    public AjaxResult signatureOutBound(@PathVariable String cgeneralhid) {
        return outboundOrderService.signatureOutBound(cgeneralhid, getLoginUser());
    }

    /**
     * 材料出库单撤回至未收货状态
     *
     * @param headerId 需要打回的单据类型
     * @return 回退结果
     */
    @PostMapping("/back/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:otherout:edit')")
    @LogB(title = "其他出库单", businessType = BusinessType.CANCELED)
    public AjaxResult addPurchase(@PathVariable("headerid") String headerId) {
        return outboundOrderService.orderBack(headerId, getLoginUser());
    }

    /**
     * 失败库存处理
     *
     * @param headerId 表头主键
     * @return 处理结果
     */
    @PostMapping("/inventory/{headerid}")
    @LogB(title = "其他出库单", businessType = BusinessType.INVENTORY)
    @PreAuthorize("@ss.hasPermi('order:otherout:audit')")
    public AjaxResult updateInventory(@PathVariable("headerid") String headerId) {
        return outboundOrderService.updateInventory(headerId, getLoginUser());
    }

    /**
     * 删除其他出库单据
     *
     * @param headerId 表头主键
     * @param reason 删除原因
     * @return 删除结果
     */
    @DeleteMapping("/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:otherout:delete')")
    @LogB(title = "其他出库单", businessType = BusinessType.INVALID)
    public AjaxResult deleteOtherOutBoundOrder(@PathVariable("headerid") String headerId,
                                               @RequestParam(name = "reason", defaultValue = "") String reason) {
        if (reason == null || reason.trim().isEmpty()) {
            return AjaxResult.error("作废原因似乎没有填写，请填写作废原因后重试！");
        }
        return outboundOrderService.deleteOtherOutBoundOrder(headerId, getLoginUser(), reason);
    }
}
